package animation.sort.gui;

import java.awt.*;
import javax.swing.*;

import animation.sort.SortAnimation;
import animation.core.AnimationController;
import animation.model.SortDataModel;

class SortToolBar
        extends JToolBar
{
    private JComboBox sortChoices;
    private JComboBox modelChoices;
    private JSlider speedSlider;
    private AbstractButton runButton;
    private AbstractButton stopButton;
    private AbstractButton stepButton;

    public SortToolBar(JComboBox sortChoices)
    {
        super(JToolBar.HORIZONTAL);

        this.sortChoices = sortChoices;
        add(sortChoices);


        final Object[] models = SortAnimation.getModels();
        modelChoices = new JComboBox(models);
        modelChoices.setMaximumRowCount(models.length);
        add(modelChoices);

        runButton = makeButton("run", "pause", AnimationController.AnimationCommand.RUN.toString());
        stopButton = makeButton("stop", null, AnimationController.AnimationCommand.STOP.toString());
        stopButton.setEnabled(false);
        stepButton = makeButton("step", null, AnimationController.AnimationCommand.STEP.toString());
        stepButton.setEnabled(true);
        addSeparator();
        speedSlider = new JSlider(0, AnimationController.MAX_SPEED, AnimationController.MAX_SPEED / 4);
        speedSlider.setMajorTickSpacing(AnimationController.MAX_SPEED / 4);
        speedSlider.setMinorTickSpacing(1);
        speedSlider.setPaintTicks(true);
        add(speedSlider);

        setFloatable(false);
        setRollover(true);
    }

    public void setControler(AnimationController animationController)
    {
        speedSlider.addChangeListener(animationController);
        runButton.addActionListener(animationController);
        stopButton.addActionListener(animationController);
        stepButton.addActionListener(animationController);
        modelChoices.addItemListener(animationController);
    }

    public Dimension getMaximumSize()
    {
        return new Dimension(Integer.MAX_VALUE, getPreferredSize().height);
    }


    void disableControls(){
        modelChoices.setEnabled(false);
    }

    void enableControls(){
        modelChoices.setEnabled(true);
    }

    protected AbstractButton makeButton(String imageName, String selectedImageName, String actionCommand)
    {
        //Create and initialize the button.
        AbstractButton button;
        if (selectedImageName == null) {
            button = new JButton();
        }
        else {
            button = new JToggleButton();
            button.setSelectedIcon(getImageIcon(selectedImageName));
        }
        button.setActionCommand(actionCommand);
        button.setIcon(getImageIcon(imageName));
        button.setBorderPainted(false);
        add(button);
        return button;
    }

    private ImageIcon getImageIcon(String imageName)
    {
        return new ImageIcon(getClass().getResource("images/" + imageName + ".png"));
    }



    public SortDataModel getModel()
    {
        return (SortDataModel) modelChoices.getSelectedItem();
    }

    public JSlider getSlider()
    {
        return speedSlider;
    }

    public void setPaused()
    {
        runButton.setSelected(false);
        stepButton.setEnabled(true);
    }

    public void setRunning() {
        runButton.setSelected(true);
        stepButton.setEnabled(false);
        stopButton.setEnabled(true);
    }

    public void setStoped() {
        runButton.setSelected(false);
        stepButton.setEnabled(true);
        stopButton.setEnabled(false);
    }
}
